package guava.study.lyh.other;

import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import org.junit.Test;

import java.util.ArrayList;

/**
 * 作者 yaohua.liu
 * 日期 2014-09-18 17:04
 * 包名 guava.study.lyh
 * 说明 ...
 */
public class BasicDataType {
    /**
     * 基本类型int的用法
     */
    @Test
    public void testInts(){
        int[] array = { 1, 2, 3, 4, 5 };
        int a = 4;

        boolean contains = Ints.contains(array, a);//true

        int indexOf = Ints.indexOf(array, a);//3

        int max = Ints.max(array);//5
        int min = Ints.min(array);//1

        int[] concat = Ints.concat(array,new int[]{1,2});//1-5 1 2
    }
    @Test
    public void objectsHashCode() {
        Objects.equal("a", "a"); // returns true
        Objects.equal(null, "a"); // returns false
        Objects.equal("a", null); // returns false
        Objects.equal(null, null); // returns true

        int result=Objects.hashCode("a", "b");//
        System.out.println(result);
    }

    /**
     * 字符串分割
     */
    @Test
    public void testSplitter() {
        Splitter splitter = Splitter
                .on(',')
                        //.onPattern("\r?\n")//按正则表达式拆分
                        //.trimResults(CharMatcher)//给定匹配器，移除结果字符串的前导匹配字符和尾部匹配字符
                .trimResults()//移除结果字符串的前导空白和尾部空白
                .omitEmptyStrings();//从结果中自动忽略空字符串

        Iterable iterable = splitter.split(",,foo,bar,,   qqux,,");
        ArrayList result= Lists.newArrayList(iterable);
        System.out.println(result);
    }

    /**
     * 字符串连接
     */
    @Test
    public void testJoiner() {
        Joiner joiner = Joiner.on(";")/*.skipNulls()*/.useForNull("**");
        String str=joiner.join("Harry", null, "Ron", "Hermione");
        System.out.println(str);
    }

    @Test
    public void testCharMatcher(){
        String str = "FirstName LastName         1\t123=456789\ts.jpg";

        System.out.println("0-1"+CharMatcher.WHITESPACE.trimAndCollapseFrom(str,' '));//去除两端的空格，并把中间的连续空格替换成单个空格
        System.out.println("0-2"+CharMatcher.JAVA_DIGIT.replaceFrom(str, "*"));//用*号替换所有数字
        System.out.println("0-3"+CharMatcher.JAVA_DIGIT.or(CharMatcher.JAVA_LOWER_CASE).retainFrom(str));// 只保留数字和小写字母

        System.out.println("1-0:"+CharMatcher.DIGIT.retainFrom(str));//剩下数字 1123456789
        System.out.println("1-1:"+CharMatcher.DIGIT.removeFrom(str));//移除数字 FirstName LastName  = s.jpg
        System.out.println("2:"+CharMatcher.JAVA_LETTER.retainFrom(str));//FirstNameLastName
        System.out.println("3:"+CharMatcher.JAVA_LETTER_OR_DIGIT.retainFrom(str));//FirstNameLastName1123456789
        System.out.println("4:"+CharMatcher.inRange('a','z').or(CharMatcher.inRange('A','Z')).or(CharMatcher.inRange('0','9')).retainFrom(str));//FirstNameLastName1123456789
        System.out.println("4.1:"+CharMatcher.inRange('a','z').and(CharMatcher.inRange('A', 'Z')).or(CharMatcher.inRange('0','9')).retainFrom(str));//1123456789
        System.out.println("4.2:"+CharMatcher.inRange('a','z').and(CharMatcher.inRange('A', 'Z')).and(CharMatcher.inRange('0', '9')).retainFrom(str));//null
        System.out.println("5:"+CharMatcher.DIGIT.countIn(str));//整数的个数
        System.out.println("6:" + CharMatcher.JAVA_LETTER.countIn(str));//字母的个数
        System.out.println("7:"+CharMatcher.anyOf("aeiou").retainFrom(str));//枚举匹配字符。如CharMatcher.anyOf(“aeiou”)匹配小写英语元音 iaeaae
        System.out.println("8:"+CharMatcher.JAVA_UPPER_CASE.retainFrom(str));//FNLN
        System.out.println("9:"+CharMatcher.JAVA_LOWER_CASE.retainFrom(str));//irstameastame
        System.out.println("10:"+CharMatcher.SINGLE_WIDTH.retainFrom(str));//获取所有单字节长度的符号
        System.out.println("11:" + CharMatcher.JAVA_ISO_CONTROL.retainFrom(str));//\t\t

        System.out.println("13-0:" + CharMatcher.anyOf("First Name").trimLeadingFrom(str));// 删除首部匹配到的字符
        System.out.println("13-1:" + CharMatcher.anyOf("FirstName").trimLeadingFrom(str));// 删除首部匹配到的字符
        System.out.println("14:" + CharMatcher.anyOf("s.jpg").trimTrailingFrom(str));//删除尾部匹配到的字符
        System.out.println("15:"+CharMatcher.anyOf("First Name").or(CharMatcher.anyOf("s.jpg")).trimFrom(str));//删除首尾匹配到的字符
    }


}
